import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

public class EventCollects {
    private final ArrayList<EventItem> eventItemCollects;
    private final String fileName;
    private final boolean isBanForFile;
    private boolean isPulled;

    public EventCollects(String fileName) {
        this.eventItemCollects = new ArrayList<>();
        this.fileName = fileName;
        this.isBanForFile = fileName.compareTo("nullFile") == 0;
        this.isPulled = false;
    }

    public String getCollectsName() {
        return fileName;
    }

    public void pullFromFile(String workDirectory) throws IOException {
        if (this.isBanForFile) {
            return;
        }

        if (this.isPulled) {
            return;
        }

        File file = new File(workDirectory + this.fileName);

        if (!file.isFile()) {
            OutputStreamWriter output = new OutputStreamWriter(
                    new FileOutputStream(workDirectory + this.fileName), StandardCharsets.UTF_8
            );
            output.write("");
            output.close();
        }

        ReadLine content = new ReadLine(Files.readString(Paths.get(workDirectory + this.fileName)));

        String line;

        line = content.readLine();
        while (line != null) {
            this.eventItemCollects.add(new EventItem(line));
            line = content.readLine();
        }

        LogHelper.messageInfo(this, "pullFromFile", "从 " + workDirectory +
                this.fileName + " 一共载入 " +this.eventItemCollects.size() + " 个条目");

        this.isPulled = true;
    }

    public void pushToFile(String workDirectory) throws IOException {
        if (isBanForFile) {
            return;
        }

        OutputStreamWriter output = new OutputStreamWriter(
                new FileOutputStream(workDirectory + this.fileName), StandardCharsets.UTF_8
        );

        for (EventItem item : this.eventItemCollects) {
            output.write(item.getWriteFormat());
        }

        output.close();

        LogHelper.messageInfo(this, "pushToFile", "成功写入 " +
                this.eventItemCollects.size() + " 个条目到 " + workDirectory + this.fileName);
    }

    public void addEventItem(long endDate, String eventName, String eventContent, boolean isLongTermEvent) {
        String formatContent = endDate + "&" + eventName + "&" + eventContent + "&" + isLongTermEvent + "\n";

        this.eventItemCollects.add(new EventItem(formatContent));
    }

    public void addEventItem(EventItem eventItem) {
        this.eventItemCollects.add(eventItem);
    }

    public void deleteEventItem(long endDate, String eventName) {
        for (int i = 0; i < this.eventItemCollects.size(); ++i) {
            EventItem item = this.eventItemCollects.get(i);
            if (item.getEndDate() == endDate && eventName.compareTo(item.getEventName()) == 0) {
                this.eventItemCollects.remove(i);
                break;
            }
        }
    }

    public boolean isFind(long endDate, String eventName) {
        for (EventItem item : this.eventItemCollects) {
            if (item.getEndDate() == endDate && eventName.compareTo(item.getEventName()) == 0) {
                return true;
            }
        }
        return false;
    }

    public ArrayList<EventItem> getEventItems() {
        return this.eventItemCollects;
    }

    public void sortAsEndDate() {
        boolean isChanged = true;
        for (int i = 0; i < this.eventItemCollects.size(); ++i) {
            if (isChanged) {
                isChanged = false;
            } else {
                break;
            }
            for (int j = 0; j < this.eventItemCollects.size() - 1; ++j) {
                EventItem item1 = this.eventItemCollects.get(j);
                EventItem item2 = this.eventItemCollects.get(j + 1);
                if (item1.getEndDate() > item2.getEndDate()) {
                    this.eventItemCollects.set(j, item2);
                    this.eventItemCollects.set(j + 1, item1);
                    isChanged = true;
                }
            }
        }
    }
}
